package com.example.finalwork.lmpl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.finalwork.DAO.PayDAO;
import com.example.finalwork.DB.Constants;
import com.example.finalwork.DB.DataBaseHelper;
import com.example.finalwork.Entity.Pay;
import com.example.finalwork.LoginActivity;

import java.util.ArrayList;
import java.util.List;

public class PayDAOlmpl implements PayDAO {
    private final DataBaseHelper mDataBaseHelper;
    public PayDAOlmpl(Context context){
        mDataBaseHelper = new DataBaseHelper(context);
    }

    @Override
    public long addPay(Pay pay) {
        SQLiteDatabase db = mDataBaseHelper.getWritableDatabase();
        ContentValues values = pay2values(pay);
        long result = db.insert(Constants.Pay_Table_Name,null,values);
        db.close();
        return result;
    }
    @Override
    public int updatePay(Pay pay){
        SQLiteDatabase db = mDataBaseHelper.getWritableDatabase();
        ContentValues values = pay2values(pay);
        int result = db.update(Constants.Pay_Table_Name,values,"pay_Id=?",new String[]{pay.getPay_Id()+""});
        db.close();
        return result;
    }
    @Override
    public int delPayById(int Id) {
        SQLiteDatabase db = mDataBaseHelper.getWritableDatabase();
        int result = db.delete(Constants.Pay_Table_Name,"pay_Id=? and pay_User=?",new String[]{Id+"", LoginActivity.data});

        db.close();
        return result;
    }

    @Override
    public List<Pay> GetAllPay() {
        List<Pay> pays = new ArrayList<>();

        SQLiteDatabase db = mDataBaseHelper.getReadableDatabase();
        Cursor cursor = db.query(Constants.Pay_Table_Name,null,null,null,null,null,null,null);
        while(cursor.moveToNext())
        {
            Pay pay = new Pay();

            int pay_Id = cursor.getInt(cursor.getColumnIndex("pay_Id"));
            pay.setPay_Id(pay_Id);

            String pay_User = cursor.getString(cursor.getColumnIndex("pay_User"));
            pay.setPay_User(pay_User);

            String pay_Name = cursor.getString(cursor.getColumnIndex("pay_Name"));
            pay.setPay_Name(pay_Name);

            String pay_Date = cursor.getString(cursor.getColumnIndex("pay_Date"));
            pay.setPay_Date(pay_Date);

            String pay_Amount = cursor.getString(cursor.getColumnIndex("pay_Amount"));
            pay.setPay_Amount(pay_Amount);

            String pay_Remark = cursor.getString(cursor.getColumnIndex("pay_Remark"));
            pay.setPay_Remark(pay_Remark);

            pays.add(pay);
        }
        db.close();
        return pays;
    }

    @Override
    public List<Pay> GetAllPaybydate() {
        List<Pay> pays = new ArrayList<>();

        SQLiteDatabase db = mDataBaseHelper.getReadableDatabase();

        String sql = "select pay_User,pay_Date,sum(pay_Amount) as allmount from " + Constants.Pay_Table_Name +  " group by " + Constants.columepaydate;
        Cursor cursor = db.rawQuery(sql,null);
        while(cursor.moveToNext())
        {
            Pay pay = new Pay();

            //int in_Id = cursor.getInt(cursor.getColumnIndex("in_Id"));
            //income.setIn_id(in_Id);

            String pay_User = cursor.getString(cursor.getColumnIndex("pay_User"));
            pay.setPay_User(pay_User);

            //String in_Name = cursor.getString(cursor.getColumnIndex("in_Name"));
            //income.setIn_Name(in_Name);

            String pay_Date = cursor.getString(cursor.getColumnIndex("pay_Date"));
            pay.setPay_Date(pay_Date);

            String pay_Amount = cursor.getString(cursor.getColumnIndex("allmount"));
            pay.setPay_Amount(pay_Amount);

            //String in_Remark = cursor.getString(cursor.getColumnIndex("in_Remark"));
            //income.setIn_Remark(in_Remark);

            pays.add(pay);
        }
        db.close();
        return pays;
    }

    private ContentValues pay2values(Pay pay) {
        //添加数据
        ContentValues values = new ContentValues();
        values.put("pay_Id",pay.getPay_Id());
        values.put("pay_User",pay.getPay_User());
        values.put("pay_Name",pay.getPay_Name());
        values.put("pay_Date",pay.getPay_Date());
        values.put("pay_Amount",pay.getPay_Amount());
        values.put("pay_Remark",pay.getPay_Remark());
        return values;
    }
}
